package com.example.seakdreamandroid;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.seakdreamandroid.sleep.AlarmClockAndPrompt;

import java.text.DecimalFormat;

public class BeforeSleepStartActivity extends AppCompatActivity {

    private ImageButton startNext;
    private CircleBarView circleBarView;
    private TextView progress;
    private TextView title;
    private LinearLayout backSpace;
    private int signal = 0;
    private Intent intent;
    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_before_sleep_start);
        //设置按钮透明度
        startNext = findViewById(R.id.startNext);
        progress = findViewById(R.id.text_progress);
        backSpace = findViewById(R.id.back_space);
        title = findViewById(R.id.before_title);
        startNext.setAlpha(0.3f);
        mMediaPlayer = MediaPlayer.create(this, R.raw.didi); //使用create方式,创建MediaPlayer对象
        mMediaPlayer.setLooping(true); // 设置是否对播放的音乐进行循环播放

        startNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signal++;
                if (mMediaPlayer.isPlaying()){
                    mMediaPlayer.pause();
                }
                if (signal % 3 == 1){
                    backSpace.setBackgroundResource(R.mipmap.back3);
                    title.setText("整理物品");
                    circleBarView.setProgressNum(100,15*60*1000);//设置动画时间
                }
                if (signal % 3 == 2){
                    backSpace.setBackgroundResource(R.mipmap.back4);
                    title.setText("睡前阅读");
                    circleBarView.setProgressNum(100,15*60*1000);//设置动画时间
                }
                if (signal % 3 == 0){
                    intent = new Intent();
                    intent.setClass(BeforeSleepStartActivity.this, AlarmClockAndPrompt.class);
                    startActivity(intent);
                    mMediaPlayer.stop();
                }
            }
        });

        circleBarView = (CircleBarView)findViewById(R.id.circle_view);
        circleBarView.setOnAnimationListener(new CircleBarView.OnAnimationListener() {
            @Override
            public String howToChangeText(float interpolatedTime, float progressNum, float maxNum) {
                DecimalFormat decimalFormat=new DecimalFormat("0.00");
                String s = decimalFormat.format(interpolatedTime * progressNum / maxNum * 100) + "%";
                if (s.equals("100.00%")){
                    s = "您已超时";
                    mMediaPlayer.start();
                }
                return s;
            }
            @Override
            public void howTiChangeProgressColor(Paint paint, float interpolatedTime, float progressNum, float maxNum) {
                LinearGradientUtil linearGradientUtil = new LinearGradientUtil(0xFFEADD68,Color.GREEN);
                paint.setColor(linearGradientUtil.getColor(interpolatedTime));
            }
        });
        circleBarView.setTextView(progress);
        circleBarView.setProgressNum(100,15*60*1000);//设置动画时间
    }
}